<?php

/**
 *  Plugin Name: IvyCat AJAX Image Slider
 *  Plugin URI: http://wordpress.org/extend/plugins/ivycat-ajax-slider/
 *  Description: Add an image slider to any page.  Meant for designers and developers; no native styles.
 *  Author: IvyCat Web Services
 *  Author URI: http://www.ivycat.com
 *  version: 1.0.5
 *  License: GNU General Public License v2.0
 *  License URI: http://www.gnu.org/licenses/gpl-2.0.html
 
 ------------------------------------------------------------------------
	IvyCat AJAX Slider, Copyright 2012 IvyCat, Inc. (admins@ivycat.com)
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 */

define( 'ICSLIDE_DIR', dirname( __FILE__ ) );
define( 'ICSLIDE_URL', str_replace( ABSPATH, site_url( '/' ), ICSLIDE_DIR ) );

class IvyCatSlider{
    
    public function __construct(){
        add_action( 'init', array( $this, 'post_type_init' ) );
        add_action( 'admin_init' , array( &$this, 'metabox_init' ) );
        add_action( 'save_post' , array( &$this, 'save_slider_metadata' ) );
        add_shortcode( 'ic_do_slider', array( &$this, 'do_slider' ) );
        add_action( 'wp_ajax_get-slides',  array( &$this, 'get_slider_images' ) );
        add_action( 'wp_ajax_nopriv_get-slides',  array( &$this, 'get_slider_images' ) );
    }
    
    public function post_type_init(){
        $labels = array(
            'name'=>__( "Sliders" ),
            'singular_name'=>__( "Slider" ),
            'add_new'=>__( 'New Slider' ),
            'add_new_item'=>__( 'Add New Slider' ),
            'edit_item'=>__( 'Edit Slider' ),
            'new_item'=>__( 'Add New Slider' ),
            'view_item'=>__( 'View Slider' ),
            'search_items'=>__( 'Search Sliders' )
        );
        
        $supports = array('title','editor','thumbnail');
        
        $args = array(
            'label'=> __( "Sliders" ),
            'labels'=>$labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true, 
            'query_var' => true,
            'rewrite' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'menu_position' => 4,
            'supports' => $supports
        );

        register_post_type( 'sliders' ,$args);
        
        $tax = array("hierarchical" => true,
            "label" => "Slider Groups",
            "singular_label" =>"Slider Group",
            "rewrite" => array('hierarchical'=>true));
        register_taxonomy( 'slider-group', 'sliders', $tax );
    }
    
    public function metabox_init(){
        add_meta_box(
            'Sliderinfo-meta',
            'Slider Data',
            array( &$this, 'slider_metabox' ),
            'sliders', 'side', 'high'
        );
    }
    
    public function slider_metabox(){
        global $post;
        $slider_order = get_post_meta( $post->ID, 'ivycat_slider_order', true);
        //$slider_link = get_post_meta( $post->ID, 'ivycat_slider_link', true);
        ?><ul><li>
            <label>Order: </label><input type="text" name="slider_order" value="<?php echo ( $slider_order ) ? $slider_order : '0'; ?>" /></li>
            </li></ul>
        <?php
    }
    
    public function save_slider_metadata(){
        if( defined( 'DOING_AJAX') ) return;
        global $post;
        update_post_meta( $post->ID, 'ivycat_slider_order', $_POST['slider_order'] );
        //update_post_meta( $post->ID, 'ivycat_slider_link', $_POST['slider_link'] );
    }
    
    public function do_slider( $atts ){
		wp_enqueue_script( 'ics-ajax-scripts', ICSLIDE_URL . '/assets/ivycat_slider_scripts.js', array( 'jquery' ) );
        wp_localize_script( 'ics-ajax-scripts', 'ICSaconn', array(
                'ajaxurl' => admin_url( 'admin-ajax.php' ),
                'themeurl' => get_bloginfo( 'stylesheet_directory' ).'/',
                'pluginurl' => ICSLIDE_URL
            )
        );
        $quantity = ( $atts['quantity'] ) ? $atts['quantity'] : 3;
        $slide_group = ( $atts['group'] ) ?  $atts['group'] : '0';
        $thumbnail_size = ( $atts['thumb_size'] ) ? $atts['thumb_size'] : 'medium';
        $slides = self::get_slides( $quantity, $slide_group, $thumbnail_size );
        ?>
        <div id="ivycat-slider">
            <div class="screen">
				<?php if( strlen( $slides[0]['slide_image'] ) > 1 ): ?>
					<img src="<?php echo $slides[0]['slide_image'] ?>" height="<?php echo $slides[0]['slide_image_height'] ?>" width="<?php echo $slides[0]['slide_image_width'] ?>" class="alignright"/>
                <?php endif; ?>
				<h3><?php echo $slides[0]['slide_title']; ?></h3>
                <div class="slider-content">
                    <?php echo $slides[0]['slide_content'] ?>
                </div>
            </div>
            <input id="slider-dets" type="hidden" name="slider-dets" value="<?php echo $quantity . '|' . $slide_group .'|' . $thumbnail_size ; ?>">
        </div>
        <?php
    }
    
    public function get_slider_images(){
        $dets = explode( '|', $_POST['slider-dets'] );
        $slides = self::get_slides( $dets[0], $dets[1], $dets[2] );
        echo json_encode( $slides );
        exit;
    }
    
    public function get_slides( $quantity , $group, $thumnail_size ){
        $thumb = explode( ',', $thumnail_size );
        if( count( $thumb ) > 1 ){
            $thumbnail_size = $thumb;
        }
        $args = array(
            "post_type" => "sliders",
            "orderby" => 'meta_value_num',
            'meta_key' => 'ivycat_slider_order',
            'order' => 'ASC',
            "posts_per_page" => $quantity
            ) ;
         if( $group != '0' ):
            $args["tax_query"] = array(
                array(
                    "taxonomy" => "slider-group",
                    "field" => "slug",
                    "terms" => $group
                )
            );
        endif;
		
        $slides = new WP_Query( $args );
        wp_reset_postdata();
        $slider_data = array();
        foreach( $slides->posts as $row ){
            $img = wp_get_attachment_image_src( get_post_thumbnail_id( $row->ID ), $thumnail_size );
            $slider_data[] = array(
                "slide_id" => $row->ID,
                "slide_title" => $row->post_title,
                "slide_content" => $row->post_content,
                "slide_image" => ($img) ? $img[0] : '',
                "slide_image_width" =>  ($img) ? $img[1]: '',
                "slide_image_height" =>  ($img) ? $img[2]: '',
                "slide_link" => get_post_meta( $row->ID, 'ivycat_slider_link', true )
            );
        }
        return $slider_data;
    }
    
} new IvyCatSlider();
